function multiCheck(groupId) {
    let check_list = $("input[host-parent-group='host-group-"+groupId+"']");
    let current = $("#checkbox_"+groupId);
//    console.log(check_list);
    if (current.is(':checked')){
        for (let i = 0; i < check_list.length; i++){
            check_list.eq(i).prop("checked",true);
        }
    } else {
        for (let i = 0; i < check_list.length; i++){
            check_list.eq(i).removeAttr('checked');
        }
    }
}

$('#checkAll').click(function () {
    let check_list = $("input[host-parent-group]");
    let current = $("#checkAll");
    if (current.is(':checked')){
        for (let i = 0; i < check_list.length; i++){
            check_list.eq(i).prop("checked",true);
        }
    } else {
        for (let i = 0; i < check_list.length; i++){
            check_list.eq(i).removeAttr('checked');
        }
    }
});

function getHosts(type){
	let hostNames = "";
	let hostIPs = "";
	$("input[host-parent-group]").each(function () {
	    if ($(this).prop("checked")) {
	        hostNames += $(this).parent().parent().children().eq(2).html() + ',';
	        hostIPs += $(this).parent().parent().children().eq(3).html() + ',';
	    }
	});
	hostNames = hostNames.substring(0, hostNames.length-1);
	hostIPs = hostIPs.substring(0, hostIPs.length-1);

	if(type=="hostsname"){
		return hostNames;
	}else{
		return hostIPs;
	}
}

function batchOrder() {
    layer.open({
        type: 2,
        skin: 'layui-layer-lan',
        title: '批量命令',
        fix: false,
        scrollbar:false,
        shadeClose: true,
        maxmin: true,
        area: ['450px', '360px'],
        content:constUrl+'/views/monhost/batchOrder.action?'
    });
}
function fileTransfer() {
	
    var IPS = getHosts("hostsname");
    if(IPS==""){
		layer.msg('请选择主机。', {
            time: 1000 //1s后自动关闭
    			});
		return ;
	}
    layer.open({
        type: 2,
        skin: 'layui-layer-lan',
        title: '文件传输',
        fix: false,
        scrollbar:false,
        shadeClose: true,
        maxmin: true,
        area: ['450px', '360px'],
        content:constUrl+'/views/monhost/fileTransfer.action?hostIP='+IPS
    });
 
}

function fileUpload(){
	
	    layer.open({
	        type: 2,
	        skin: 'layui-layer-lan',
	        title: '文件传输',
	        fix: false,
	        scrollbar:false,
	        shadeClose: true,
	        maxmin: true,
	        area: ['450px', '360px'],
	        content:constUrl+'/views/monhost/fileUpload'
	    });
	
}

function formClose(){
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    //layer.msg(index);
    parent.layer.close(index); //再执行关闭 
}

function viewInstallers(hostIP) {
    layer.open({
        type: 2,
        skin: 'layui-layer-lan',
        title: '查看进程',
        fix: false,
        scrollbar:false,
        shadeClose: true,
        maxmin: true,
        area: ['450px', '360px'],
        content:constUrl+'/views/monhost/viewInstallers.action?hostIP='+hostIP
    });
}
function viewProcess(hostIP) {
    layer.open({
        type: 2,
        skin: 'layui-layer-lan',
        title: '查看安装程序',
        fix: false,
        scrollbar:false,
        shadeClose: true,
        maxmin: true,
        area: ['700px', '600px'],
        content:constUrl+'/views/monhost/viewProcess.action?hostIP='+hostIP
    });
}

function checkUsb(hostIP){
	layer.open({
        type: 2,
        skin: 'layui-layer-lan',
        title: '查看USB记录',
        fix: false,
        scrollbar:false,
        shadeClose: true,
        maxmin: true,
        area: ['700px', '600px'],
        content:constUrl+'/views/monhost/viewUsb.action?hostIP='+hostIP
    });
}

function closeHosts() {
	var IPS = getHosts();
	if(IPS==""){
		layer.msg('请选择主机。', {
            time: 1000 //1s后自动关闭
    			});
		return;
	}
	layer.confirm('确定要关闭该主机吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
    	$.ajax({
            type:"GET",
            url:constUrl+"ws/remote/host/shutdown",
            dataType:"json",
            data: {
                IPS:IPS
            },
            success:function(data){
            	console.log(data);
            	layer.msg('关机成功', {
                    time: 1000 //1s后自动关闭
            			});
            }
        });
    }, function(){
        layer.msg('取消关闭', {
            time: 1000 //1s后自动关闭
        });
    });
}

function closeHost(hostId) {
    layer.confirm('确定要关闭该主机吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
    	$.ajax({
            type:"GET",
            url:constUrl+"ws/remote/host/shutdown",
            dataType:"json",
            data: {
                IPS:hostId
            },
            success:function(data){
            	console.log(data);
            	layer.msg('关机成功', {
                    time: 1000 //1s后自动关闭
            			});
            }
        });
    }, function(){
        layer.msg('取消关闭', {
            time: 1000 //1s后自动关闭
        });
    });
}



function setModel() {
    var IPS = getHosts("hostsname");
    if(IPS==""){
		layer.msg('请选择主机。', {
            time: 1000 //1s后自动关闭
    			});
		return;
	}
    layer.open({
        type: 2,
        skin: 'layui-layer-lan',
        title: '模式选择',
        fix: false,
        scrollbar:false,
        shadeClose: true,
        maxmin: true,
        area: ['450px', '360px'],
        content:constUrl+'/views/monhost/patternChose?IPS='+IPS
    });
}

function getModeFunctions(modes,IPS){
	var rt=null;
	for(var i=0;i<modes.length;i++){
		rt = rt +",btn["+i+1+"]:function(){changeMode("+modes[i]+","+IPS+");";
	}
//	alert(rt);
	return rt;
} 

function changeMode(modeName,IPS){

	layer.confirm('确定要切换模式吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
    	$.ajax({
    		type:"GET",
            url:constUrl+"ws/remote/mode/change",
            dataType:"json",
            data: {
            	startType: modeName,
            	startIPS:IPS
               
            },
            success:function(data){
            	layer.msg(data.data, {
                    time: 1500 //1s后自动关闭
                });
            	formClose();
            },error:function(data){
            	layer.msg(data.data, {
                    time: 1500 //1s后自动关闭
                });
            	formClose();
            }
    	});
       // layer.closeAll('dialog');
    	
    }, function(){
        layer.msg('取消切换', {
            time: 1000 //1s后自动关闭
        });
    });
	
}

function downMode() {
	var IPS = getHosts("hostsname");
	if(IPS==""){
		layer.msg('请选择主机。', {
            time: 1000 //1s后自动关闭
    			});
		return;
	}
	layer.confirm('确定要关闭节点运行中的模式吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
    	$.ajax({
            type:"GET",
            url:constUrl+"ws/remote/mode/change",
            dataType:"json",
            data: {
            	startIPS:IPS
            },
            success:function(data){
//            	console.log(data);
            	layer.msg(data.data, {
                    time: 2000 //1s后自动关闭
            			});
            }
        });
    }, function(){
        layer.msg('取消关闭', {
            time: 1000 //1s后自动关闭
        });
    });
}

function telent(){
    layer.prompt({
        formType: 2,
        title: '请输入远程命令',
        area: ['800px', '350px'] //自定义文本域宽高
    }, function(value, index, elem){
        alert(value); //得到value
        layer.close(index);
    }, function(){
    });
}

function softwareDeployment() {
	var IPS = getHosts();
	if(IPS==""){
		layer.msg('请选择主机。', {
            time: 1000 //1s后自动关闭
    			});
		return;
	}
    layer.open({
        type: 2,
        skin: 'layui-layer-lan',
        title: '软件部署',
        fix: false,
        scrollbar:false,
        shadeClose: true,
        maxmin: true,
        area: ['450px', '360px'],
        content:constUrl+'/views/monhost/softwareDeployment.action?hostIP='+IPS
    });
}
function setMainSSH () {
    layer.prompt({
        formType: 2,
        title: '请配置SSH',
        area: ['800px', '350px'] //自定义文本域宽高
    }, function(value, index, elem){
        alert(value); //得到value
        layer.close(index);
    }, function(){
    });
}
